﻿/*
Copyright (c) 2008 Martin Legris

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software 
without restriction, including without limitation the rights to use, copy, modify, 
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to the following 
conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@ignore
*/
package newcommerce.lastfm.api 
{	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
		
	import newcommerce.lastfm.api.clients.*;	
	
	/**
	 * The starting point for using this library. This class is a singleton
	 * @usage var client:LastFMClient = LastFMClient.getInstance();
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class LastFMClient
	{
		/**
		 * The apiKey given to you by LastFM
		 */
		public static var apiKey:String = "YOUR KEY";
		
		/**
		 * The shared secret given to you by LastFM
		 */
		
		public static var secret:String = "YOUR SECRET";
		
		/**
		 * The base url for all communications with the server
		 */
		public static var baseUrl:String = "http://ws.audioscrobbler.com/2.0/";
		
		/**
		 * The session key generated at authentication
		 */
		public static var sessionKey:String;
		
		protected var _album:AlbumClient;
		protected var _artist:ArtistClient;
		protected var _auth:AuthClient;
		protected var _event:EventClient;
		protected var _geo:GeoClient;
		protected var _group:GroupClient;
		protected var _library:LibraryClient;
		protected var _playlist:PlaylistClient;
		protected var _tag:TagClient;
		protected var _tasteometer:TasteometerClient;
		protected var _track:TrackClient;
		protected var _venue:VenueClient;
		protected var _user:UserClient;
		protected var _scrobble:ScrobbleClient;
		
		/**
		 * Client used for all Album related methods
		 */
		public function get album():AlbumClient { return _album == null ? _album = new AlbumClient() : _album; }
		
		/**
		 * Client used for all Artist related methods
		 */
		public function get artist():ArtistClient { return _artist == null ? _artist = new ArtistClient() : _artist; }
		
		/**
		 * Client used for all Auth related methods
		 */
		public function get auth():AuthClient { return _auth == null ? _auth = new AuthClient() : _auth; }
		
		/**
		 * Client used for all Event related methods
		 */
		public function get event():EventClient { return _event == null ? _event = new EventClient() : _event; }
		
		/**
		 * Client used for all Geo related methods
		 */
		public function get geo():GeoClient { return _geo == null ? _geo = new GeoClient() : _geo; }
		
		/**
		 * Client used for all Group related methods
		 */
		public function get group():GroupClient { return _group == null ? _group = new GroupClient() : _group; }
		
		/**
		 * Client used for all Library related methods
		 */
		public function get library():LibraryClient { return _library == null ? _library = new LibraryClient() : _library; }
		
		/**
		 * Client used for all Playlist related methods
		 */
		public function get playlist():PlaylistClient { return _playlist == null ? _playlist = new PlaylistClient() : _playlist; }
		
		/**
		 * Client used for all Tag related methods
		 */
		public function get tag():TagClient { return _tag == null ? _tag = new TagClient() : _tag; }
		
		/**
		 * Client used for all Tasteometer related methods
		 */
		public function get tasteometer():TasteometerClient { return _tasteometer == null ? _tasteometer = new TasteometerClient() : _tasteometer; }
		
		/**
		 * Client used for all Track related methods
		 */
		public function get track():TrackClient { return _track == null ? _track = new TrackClient() : _track; }
		
		/**
		 * Client used for all Venue related methods
		 */
		public function get venue():VenueClient { return _venue == null ? _venue = new VenueClient() : _venue; }
		
		/**
		 * Client used for all User related methods
		 */
		public function get user():UserClient { return _user == null ? _user = new UserClient() : _user; }
		
		/**
		 * Client used for all Scrobbling related methods
		 */		
		public function get scrobble():ScrobbleClient { return _scrobble == null ? _scrobble = new ScrobbleClient() : _scrobble; }
		
		protected static var _instance:LastFMClient;
		
		/**
		 * This class being a singleton, use this function to get access to its only instance
		 * @return LastFMClient
		 * @example Example of method in use:  
		 * <listing version="3.0" > 
		 * 
		 * 
		 * </listing>
		 */
		public static function getInstance():LastFMClient
		{
			if (_instance == null)
				_instance = new LastFMClient();

			return _instance;
		}
		
		public function LastFMClient() 
		{
			super();			
		}	
		
		/**
		 * This functions returns true if these is a client session available right now
		 * @return true if there is a authenticated session in progress
		 */
		public function isLoggedIn():Boolean
		{
			return sessionKey != "" && sessionKey != null;
		}		
	}
}